// copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
// see COPYRIGHT for reuse legalities
//

#import "RIBMotionBegin.h"

@implementation RIBMotionBegin


+ initialize { return [RIBMotionBegin setVersion:1], self; }

- init
{
  [super init];

  nTimes = 0;
  times = NULL;
  
  return self;
}

- awake
{
  [super awake];

  nTimes = 0;
  times = NULL;
  
  return self;

}

- free
{
  if (nTimes) {  free(times); }
  return [super free];
}

- setNTimes:(RtInt)newNTimes timeVector:(RtFloat *)timeVector
{
  nTimes = newNTimes;
  times = timeVector;
  return self;
}

- (RtInt)nTimes { return nTimes; }
- (float *)times { return times; }

- renderSelf:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  RiMotionBeginV(n, times);

  return self;
}

- (unsigned long int)maxSampleBandwidth {  return ([super maxSampleBandwidth] + (unsigned long int)(sizeof(RtInt) + (nTimes * sizeof(RtFloat)))); }


- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "MotionBegin {"); 
   for (i = 0; i < nTimes; i++)
   {  NXPrintf(stream, "%f ", times[i]);
   }
   NXPrintf(stream, "};"); 
   return self;
}

#define typeVector "d"
#define typeValues &nTimes

- read:(NXTypedStream*)stream 
{
    int version, i;
    [super read:stream];

    version = NXTypedStreamClassVersion(stream,"RIBMotionBegin");
    if (version == 0) NXReadTypes(stream, "i", &version), version=1;
    if (version == 1)
    {  NXReadTypes(stream, typeVector, typeValues);
       if (nTimes)
       {  times = (float *)malloc(sizeof(float)*nTimes);
          for (i = 0; i < nTimes; i++)
          NXReadTypes(stream, "f", times[i]);
       }
       else
       {  times = NULL;
       }
    } 
    else 
    {
    }
    return self;
}

- write:(NXTypedStream*)stream 
{
    int  i;


    [super write:stream];

    NXWriteTypes(stream, typeVector, typeValues);
    if (nTimes)
    {  for (i = 0; i < nTimes; i++)
       NXWriteTypes(stream, "f", times[i]);
    }
    else
    {  times = NULL;
    }

    return self;
}

@end
